package Biz;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;

import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import Entity.PcrTube;
import Entity.User;

/**
 * Session Bean implementation class inputTestedBean
 */
@Stateful
@LocalBean
public class inputTestedBean implements inputTestedBeanLocal {

    /**
     * Default constructor. 
     */
    @PersistenceContext(unitName = "PCR_System")
    private EntityManager manager;
    
	HashSet<User>Testeds=new HashSet<User>();
	PcrTube tube=new PcrTube();
    public inputTestedBean() {
        // TODO Auto-generated constructor stub
    }
    public void addStudent(String ID)
    {
    	User u=manager.find(User.class,ID);
    	if(u!=null&&!Testeds.contains(u))
    	{
    		Testeds.add(u);
    	}
    }
    public void deleteStudent(String ID)
    {
    	for(User u:Testeds)
    	{
    		if(u.getId().equals(ID))
    		{
    			Testeds.remove(u);
    			return;
    		}
    	}
    }
    public Set<User> getTesteds()
    {
    	return Testeds;
    }
    public boolean setTubeID(String ID)
    {
    	System.out.println(manager);
    	if(manager.find(PcrTube.class, ID)==null)
    	{
        	tube.setId(ID);
        	return true;
    	}
    	tube.setId(null);
    	return false;
    }
    public String getTubeID()
    {
    	return tube.getId();
    }
    public void submit(){
    	tube.setUsers(new LinkedList<User>(Testeds));
    	tube.setStatus("no_result");
    	SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
    	tube.setCollect_time(ft.format(new Date()));
    	manager.persist(tube);
    }
}
